package com.wetao.lib_common.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;

import com.wetao.lib_common.ConstantNotice;
import com.wetao.lib_common.greendao.DBUtils;

import java.io.File;

public class AppInstallUtil {

    public final static int REQUEST_CODE_APP_INSTALL=1101;

    /**
     * 安装apk
     */
    public static void installApk(Context context, String apkname) {


        System.out.println("apkname="+apkname);

        File file = new File(apkname);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // 由于没有在Activity环境下启动Activity,设置下面的标签
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) { //判读版本是否在7.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean hasInstallPermission = isHasInstallPermissionWithO(context);
                if (!hasInstallPermission) {
                    startInstallPermissionSettingActivity(context);
                    return;
                }
            }

            //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
            String authority=context.getApplicationContext().getPackageName()+ ".fileprovider";

            System.out.println("authority---"+authority);
            Uri apkUri =
                    FileProvider.getUriForFile(context, authority, file);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件

            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

        }else{
            System.out.println("setDataAndType---");
            intent.setDataAndType(Uri.fromFile(file),
                    "application/vnd.android.package-archive");


        }

        try{
            //DBUtils.getInstance(context).deleteNoticeItemByNoticeId(ConstantNotice.NOTICE_VERSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }





    @RequiresApi(api = Build.VERSION_CODES.O)
    public static boolean isHasInstallPermissionWithO(Context context){
        if (context == null){
            return false;
        }
        return context.getPackageManager().canRequestPackageInstalls();
    }



    /**
     * 开启设置安装未知来源应用权限界面
     * @param context
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void startInstallPermissionSettingActivity(Context context) {
        if (context == null){
            return;
        }
        //DBUtils.getInstance(context).deleteNoticeItemByNoticeId(ConstantNotice.NOTICE_VERSION);
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + context.getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

}
